package com.ruoyi.web.controller.system;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.req.DoseGetInfoReq;
import com.ruoyi.system.domain.req.UpdateDoseSampleReq;
import com.ruoyi.system.domain.res.dose.DoseDay_menu;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Dose;
import com.ruoyi.system.service.IDoseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报量单Controller
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@RestController
@RequestMapping("/system/dose")
public class DoseController extends BaseController {
    @Resource
    private IDoseService doseService;

    /**
     * 查询报量单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:dose:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dose dose) {
        startPage();
        LoginUser loginUser = getLoginUser();
        Integer type = loginUser.getUser().getDept().getType();
        if (type != null && type.equals(1)) {
            dose.setSchoolId(loginUser.getDeptId());
        }
        List<Dose> list = doseService.selectDoseList(dose);
        return getDataTable(list);
    }

    /**
     * 导出报量单列表
     */
    //@PreAuthorize("@ss.hasPermi('system:dose:export')")
    @Log(title = "报量单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dose dose) {
        List<Dose> list = doseService.selectDoseList(dose);
        ExcelUtil<Dose> util = new ExcelUtil<Dose>(Dose.class);
        util.exportExcel(response, list, "报量单数据");
    }

    /**
     * 获取报量单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:dose:list')")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(DoseGetInfoReq req) {
        LoginUser loginUser = getLoginUser();
        Integer type = loginUser.getUser().getDept().getType();
        if (type != null && type.equals(1)) {
            req.setSchoolId(loginUser.getDeptId());
        }
        return success(doseService.selectDoseById(req));
    }


    /**
     * 新增报量单
     */
    //@PreAuthorize("@ss.hasPermi('system:dose:add')")
    @Log(title = "报量单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dose dose) {
        LoginUser loginUser = getLoginUser();
        dose.setSchoolId(loginUser.getDeptId());
        return toAjax(doseService.insertDose(dose));
    }

    /**
     * 修改报量单
     */
    //@PreAuthorize("@ss.hasPermi('system:dose:edit')")
    @Log(title = "报量单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dose dose) {
        return toAjax(doseService.updateDose(dose));
    }

    /**
     * 删除报量单
     */
    //@PreAuthorize("@ss.hasPermi('system:dose:remove')")
    @Log(title = "报量单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(doseService.deleteDoseByIds(ids));
    }

    /**
     * 修改留样图片
     */
    @PutMapping("/updateDoseSample")
    public AjaxResult updateDoseSample(@RequestBody UpdateDoseSampleReq req) {
        return toAjax(doseService.updateDoseSample(req));
    }

    /**
     * 根据日期查询菜谱列表
     */
    @GetMapping("/selectDoseDay")
    @Anonymous
    public AjaxResult selectDoseDay(String week) {
        return AjaxResult.success(doseService.selectDoseDayList(week));
    }
}
